mask_siteid_sampling <- site_protocol_quanti[
  site_protocol_quanti$variable == "year" &
    site_protocol_quanti$n >= 10,
  ]$siteid

mask_siteid_protocol <- site_protocol_quali[
  site_protocol_quali$unitabundance %in% c("Count", "Ind.100m2"), ]$siteid

mask_siteid <- mask_siteid_sampling[
    mask_siteid_sampling %in% mask_siteid_protocol]
dir_plot <- here("doc", "fig", "raw_data")
if (!dir.exists(dir_plot)) {
  dir.create(dir_plot)
}

abun_rich_nested <- filtered_dataset$abun_rich_op %>%
  nest_by(siteid)

library(furrr)
plan(multisession, workers = 3)
future_walk2(abun_rich_nested$data, abun_rich_nested$siteid,
  function (x, y, ...) {

    png(paste0(dir_plot, "/species_nb_site_", y, ".png"), width = 500, height = 500*1/1.6)

    p <- plot_community_data(
        dataset = x, y = "species_nb", x = "year", title = y)

    print(p)

    dev.off()
  })

future_walk2(abun_rich_nested$data, abun_rich_nested$siteid,
  function (x, y, ...) {

    png(paste0(dir_plot, "/species_nb_site_", y, ".png"), width = 500, height = 500*1/1.6)

    p <- plot_community_data(
      dataset = x, y = "total_abundance", x = "year", title = y)

    print(p)

    dev.off()
  })

1 Maps

library(mapview)
#> Warning in (function (n) : input string '/home/alain/Téléchargements/R-4.0.5/
#> library/methods/R/methods' cannot be translated to UTF-8, is it valid in
#> 'ANSI_X3.4-1968'?
#> Warning in (function (n) : input string '/home/alain/Téléchargements/R-4.0.5/
#> library' cannot be translated to UTF-8, is it valid in 'ANSI_X3.4-1968'?
#> Warning: Tables de méthodes multiples trouvées pour 'crop'
#> Warning: Tables de méthodes multiples trouvées pour 'extend'
library(leafpop)
tar_load(filtered_dataset)
loc <- filtered_dataset$location %>%
  left_join(filtered_dataset$site_quali, by = "siteid") %>%
  st_as_sf(coords = c("longitude", "latitude"),
  crs = 4326)
site_richness <- filtered_dataset$measurement %>%
  group_by(siteid) %>%
  summarise(tot_nb_species = length(unique(species)))

op_richness_summary <-
  filtered_dataset$abun_rich_op %>%
  group_by(siteid) %>%
  summarise(enframe(summary_distribution(species_nb)), .groups = "drop") %>%
  pivot_wider(names_from = "name", values_from = "value") %>%
  rename(median_richness = median)
var_map_view <- c("siteid", "protocol", "unitabundance", "unitbiomass", "min",
  "max", "completeness", "tot_nb_richness", "median_richness")
loc2 <- loc %>%
  left_join(op_richness_summary, by = "siteid") %>%
  select(any_of(var_map_view)) %>%
  left_join(site_richness, by = "siteid") %>%
  select(any_of(var_map_view))
mapView(loc2, zcol = "protocol")
get_file_plot_in_tbl <- function(
  directory = NULL,
  str_file_to_match = NULL,
  regex_pattern = NULL) {

  file_plot_site <- list.files(directory, full.names = TRUE)
  filtered_file_plot_site <- file_plot_site[
    str_detect(file_plot_site, str_file_to_match)]

  names(filtered_file_plot_site) <- str_extract(filtered_file_plot_site,
    regex_pattern)
  filtered_file_plot_site <- enframe(
    filtered_file_plot_site,
    name = "siteid",
    value = "file"
  )

}

abun_file_plot_site_tbl <- get_file_plot_in_tbl(
  directory = "fig/raw_data",
  str_file_to_match = "tot_abun",
  regex_pattern = "S\\d+"

)

richness_file_plot_site_tbl <- get_file_plot_in_tbl(
  directory = dir_plot,
  str_file_to_match = "species_nb",
  regex_pattern = "S\\d+") %>%
  rename(richness = file)
loc2 <- loc %>%
  select(siteid, protocol) %>%
  left_join(abun_file_plot_site_tbl, by = "siteid") %>%
  left_join(richness_file_plot_site_tbl, by = "siteid")
m <- mapView(loc2, zcol = "protocol",
  popup = popupImage(loc2$file)
)
mapshot(m, url = "map_abundance.html",
  selfcontained = FALSE,
)
m2 <- mapView(loc2, zcol = "protocol",
  popup = popupImage(loc2$richness)
)
mapshot(m2, url = "map_richness.html",
  selfcontained = FALSE
)
trends_data <- abun_rich_op %>%
  left_join(op_protocol, by = "op_id") %>%
  filter(siteid %in% mask_siteid) %>%
  mutate(
    log_total_abundance = log(total_abundance),
    log_species_nb = log(species_nb)
  )
plot_trends <- trends_data %>%
  group_by(siteid) %>%
  nest() %>%
  ungroup() %>%
  slice_sample(n = 100) %>%
  mutate(
    p_abun = map2(data, siteid,
      ~plot_community_data(
        dataset = .x, y = "total_abundance", x = "year", title = .y)),
    p_rich = map2(data, siteid,
      ~plot_community_data(
        dataset = .x, y = "species_nb", x = "year", title = .y),
    )
  )

1.1 Total abundance

n_plot_by_batch <- 8
map(
  split(
    seq_len(nrow(plot_trends)),
    1:floor(nrow(plot_trends) / n_plot_by_batch) + 1),
  ~plot_grid(plotlist = plot_trends[.x, ]$p_abun)
  )
#> Warning in split.default(seq_len(nrow(plot_trends)), 1:floor(nrow(plot_trends)/
#> n_plot_by_batch) + : la taille de données n'est pas un multiple de la variable
#> découpée
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : pseudoinverse used at 2007
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : neighborhood radius 2
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : reciprocal condition number 0
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used at
#> 2007
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius 2
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : reciprocal condition
#> number 0
#> $`2`

#> 
#> $`3`

#> 
#> $`4`

#> 
#> $`5`

#> 
#> $`6`

#> 
#> $`7`

#> 
#> $`8`

#> 
#> $`9`

#> 
#> $`10`

#> 
#> $`11`

#> 
#> $`12`

#> 
#> $`13`

1.2 Species richness

map(
  split(
    seq_len(nrow(plot_trends)),
    1:floor(nrow(plot_trends) / n_plot_by_batch) + 1
    ),
  ~plot_grid(plotlist = plot_trends[.x, ]$p_rich)
  )
#> Warning in split.default(seq_len(nrow(plot_trends)), 1:floor(nrow(plot_trends)/
#> n_plot_by_batch) + : la taille de données n'est pas un multiple de la variable
#> découpée
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : pseudoinverse used at 2007
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : neighborhood radius 2
#> Warning in simpleLoess(y, x, w, span, degree = degree, parametric =
#> parametric, : reciprocal condition number 0
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : pseudoinverse used at
#> 2007
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : neighborhood radius 2
#> Warning in predLoess(object$y, object$x, newx = if
#> (is.null(newdata)) object$x else if (is.data.frame(newdata))
#> as.matrix(model.frame(delete.response(terms(object)), : reciprocal condition
#> number 0
#> $`2`

#> 
#> $`3`

#> 
#> $`4`

#> 
#> $`5`

#> 
#> $`6`

#> 
#> $`7`

#> 
#> $`8`

#> 
#> $`9`

#> 
#> $`10`

#> 
#> $`11`

#> 
#> $`12`

#> 
#> $`13`

1.3

tar_load(toy_dataset)
unique(toy_dataset$siteid)
#> [1] "S8633"  "S11138" "S534"   "S529"   "S11219"
plot_temporal_biomass <- function (bm_data = NULL, biomass_var = NULL, com = NULL, .log = FALSE) {

  #main_title <- paste0("Stab = ", round(1/(sync$cv_com), 2),", ", "Sync = ",
    #round(sync$synchrony, 2),", ", "CVsp = ", round(sync$cv_sp, 2))
  sym_bm_var <- rlang::sym(biomass_var)
  # Total
  total_biomass <- bm_data %>% 
  group_by(date) %>%
  summarise(!!sym_bm_var := sum(!!sym_bm_var, na.rm = FALSE))
  
  p <- bm_data %>%
    mutate(label = if_else(date == max(date), as.character(species), NA_character_)) %>%
  ggplot(aes_string(x = "date", y = biomass_var, color = "species")) + 
  geom_line() +
  lims(y = c(0, max(total_biomass[[biomass_var]]))) +
  labs(
  #title = main_title, subtitle = paste0("Station: ", station),
    y = "Biomass (g)", x = "Sampling date"
  ) +
  ggrepel::geom_label_repel(aes(label = label),
    size = 2.5, nudge_x = 1, na.rm = TRUE) 
  
  # Add total biomass
  p2 <- p +
    geom_line(data = total_biomass, aes(color = "black", size = 3)) +
    theme(legend.position = "none")

  # Add summary: richness, connectance, stab, t_lvl, sync, cv_sp 
  com %<>%
    mutate_if(is.double, round(., 2))

  label <- paste(
    "S = ", com$bm_std_stab,
    "sync = ", com$sync,
    "CVsp = ", com$cv_sp,
    "R = ", com$rich_tot_std,
    "C = ", com$ct,
    "Tlvl = ", com$t_lvl
  ) 

  p3 <- p2 +
    annotate("text", x = median(total_biomass$date),
      y = 15, label = label)

  if (.log) {
    p3 <- p3 + scale_y_log10() 
  }

  return(p3)
}

ti <- toy_dataset %>%
  filter(siteid == unique(toy_dataset$siteid)[2])
plot_population <- function (dataset = NULL, y_var = NULL, time_var = NULL) {

  sym_y_var <- rlang::sym(y_var)
  sym_time_var <- rlang::sym(time_var)
  # Total
  total_dataset <- dataset %>%
  group_by(!!sym_time_var) %>%
  summarise(!!sym_y_var := sum(!!sym_y_var, na.rm = FALSE))
  
  p <- dataset %>%
    mutate(label = if_else(!!sym_time_var == max(!!sym_time_var), as.character(species), NA_character_)) %>%
  ggplot(aes_string(x = time_var, y = y_var, color = "species")) + 
  geom_line() +
  lims(y = c(0, max(total_dataset[[y_var]]))) +
  labs(
  #title = main_title, subtitle = paste0("Station: ", station),
    y = "Biomass (g)", x = "Sampling time_var"
  ) +
  ggrepel::geom_label_repel(aes(label = label),
    size = 2.5, nudge_x = 1, na.rm = TRUE)
  
  # Add total biomass
  p2 <- p +
    geom_line(data = total_dataset, aes(color = "black", size = 3)) +
    theme(legend.position = "none")
  return(p2)

}

plot_population(dataset = ti, y_var = "abundance", time_var = "year")
#> Warning: ggrepel: 12 unlabeled data points (too many overlaps). Consider
#> increasing max.overlaps

plot_temporal_population(com = ti, ribbon = FALSE)

p <- plot_temporal_population(com = ti, ribbon = TRUE)

GeomRibbon$handle_na <- function(data, params) {  data }
p$data %>%
  ggplot(
    aes(y = abundance, ymin = ymin, ymax = ymax, x = year,
      fill = species)
    ) +
  geom_ribbon()
set.seed(1)

test <- data.frame(x = rep(1:10, 3), y = abs(rnorm(30)), z = rep(LETTERS[1:3],
    10)) %>% arrange(x, z)

test[test$x == 4, "y"] <- NA

test$ymax <- test$y
test$ymin <- 0
zl <- unique(test$z)
for (i in 2:length(zl)) {
    zi <- test$z == zl[i]
    zi_1 <- test$z == zl[i - 1]
    test$ymin[zi] <- test$ymax[zi_1]
    test$ymax[zi] <- test$ymin[zi] + test$ymax[zi]
}


# fix GeomRibbon
GeomRibbon$handle_na <- function(data, params) {  data }

ggplot(test, aes(x = x, y=y, ymax = ymax, ymin = ymin, fill = z)) +
  geom_ribbon()
  • Be careful to multiple fishing per year
toy_dataset %>%
  group_by(siteid, year, species) %>%
  summarise(test=n()>1) %>%
  filter(test)
pop_trends <- toy_dataset %>%
  filter(!siteid %in% c("S534", "S8633")) %>%
  group_by(siteid) %>%
  nest() %>%
  mutate(
    p_pop = map(data, ~plot_temporal_population(com = .x, ))
  )
#> Note: Using an external vector in selections is ambiguous.
#> ℹ Use `all_of(species_var)` instead of `species_var` to silence this message.
#> ℹ See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
#> This message is displayed once per session.
#> Note: Using an external vector in selections is ambiguous.
#> ℹ Use `all_of(y_var)` instead of `y_var` to silence this message.
#> ℹ See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
#> This message is displayed once per session.
#> Note: Using an external vector in selections is ambiguous.
#> ℹ Use `all_of(species)` instead of `species` to silence this message.
#> ℹ See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
#> This message is displayed once per session.
pop_trends$p_pop
#> [[1]]
#> Warning: Removed 280 rows containing missing values (position_stack).

#> 
#> [[2]]

#> 
#> [[3]]
#> Warning: Removed 104 rows containing missing values (position_stack).

1.4 Analysis

1.5 Reproducibility

Reproducibility receipt

## datetime
Sys.time()
#> [1] "2022-01-19 22:19:03 CST"

## repository
if(requireNamespace('git2r', quietly = TRUE)) {
  git2r::repository()
} else {
  c(
    system2("git", args = c("log", "--name-status", "-1"), stdout = TRUE),
    system2("git", args = c("remote", "-v"), stdout = TRUE)
  )
}
#> Local:    main /home/alain/Documents/post-these/isu/RivFishTimeBiodiversityFacets
#> Head:     [8eba9e1] 2022-01-18: add trends vs classification

## session info
sessionInfo()
#> R version 4.0.5 (2021-03-31)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Debian GNU/Linux 10 (buster)
#> 
#> Matrix products: default
#> BLAS:   /home/alain/.Renv/versions/4.0.5/lib/R/lib/libRblas.so
#> LAPACK: /home/alain/.Renv/versions/4.0.5/lib/R/lib/libRlapack.so
#> 
#> locale:
#>  [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=fr_FR.UTF-8        LC_COLLATE=fr_FR.UTF-8    
#>  [5] LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=fr_FR.UTF-8   
#>  [7] LC_PAPER=fr_FR.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] leafpop_0.1.0           mapview_2.10.0          future_1.21.0          
#>  [4] vegan_2.5-7             lattice_0.20-41         permute_0.9-5          
#>  [7] codyn_2.0.5             janitor_2.1.0           viridis_0.5.1          
#> [10] viridisLite_0.3.0       cowplot_1.1.1           rnaturalearthdata_0.1.0
#> [13] rnaturalearth_0.1.0     sf_1.0-4                rmarkdown_2.11         
#> [16] scales_1.1.1            kableExtra_1.3.1        here_1.0.1             
#> [19] lubridate_1.7.9.2       magrittr_2.0.1          forcats_0.5.1          
#> [22] stringr_1.4.0           dplyr_1.0.4             purrr_0.3.4            
#> [25] readr_2.1.1             tidyr_1.1.2             tibble_3.1.6           
#> [28] ggplot2_3.3.3           tidyverse_1.3.0         tarchetypes_0.3.2      
#> [31] targets_0.8.1           conflicted_1.1.0       
#> 
#> loaded via a namespace (and not attached):
#>   [1] uuid_1.0-3              readxl_1.3.1            backports_1.2.1        
#>   [4] systemfonts_1.0.0       igraph_1.2.6            sp_1.4-5               
#>   [7] splines_4.0.5           gmp_0.6-2.1             crosstalk_1.1.1        
#>  [10] listenv_0.8.0           leaflet_2.0.4.1         usethis_2.0.1          
#>  [13] digest_0.6.27           htmltools_0.5.1.1       leaflet.providers_1.9.0
#>  [16] fansi_0.5.0             memoise_2.0.0           cluster_2.1.1          
#>  [19] tzdb_0.2.0              globals_0.14.0          modelr_0.1.8           
#>  [22] svglite_2.0.0           bench_1.1.1             colorspace_2.0-0       
#>  [25] ggrepel_0.9.1           rvest_0.3.6             haven_2.3.1            
#>  [28] xfun_0.28               leafem_0.1.6            callr_3.7.0            
#>  [31] crayon_1.4.2            jsonlite_1.7.2          brew_1.0-6             
#>  [34] glue_1.5.1              gtable_0.3.0            webshot_0.5.2          
#>  [37] untb_1.7-4              DBI_1.1.1               Rcpp_1.0.6             
#>  [40] units_0.6-7             stats4_4.0.5            htmlwidgets_1.5.3      
#>  [43] httr_1.4.2              wk_0.5.0                ellipsis_0.3.2         
#>  [46] pkgconfig_2.0.3         partitions_1.10-4       farver_2.0.3           
#>  [49] sass_0.3.1              dbplyr_2.1.0            utf8_1.2.2             
#>  [52] tidyselect_1.1.1        labeling_0.4.2          rlang_0.4.12           
#>  [55] polynom_1.4-0           munsell_0.5.0           cellranger_1.1.0       
#>  [58] tools_4.0.5             cachem_1.0.4            cli_3.1.0              
#>  [61] generics_0.1.0          broom_0.7.4             mathjaxr_1.4-0         
#>  [64] evaluate_0.14           fastmap_1.1.0           yaml_2.2.1             
#>  [67] processx_3.5.2          knitr_1.36              fs_1.5.1               
#>  [70] s2_1.0.7                satellite_1.0.4         nlme_3.1-152           
#>  [73] xml2_1.3.2              compiler_4.0.5          rstudioapi_0.13        
#>  [76] png_0.1-7               e1071_1.7-4             reprex_1.0.0           
#>  [79] bslib_0.2.4             stringi_1.7.6           highr_0.9              
#>  [82] ps_1.6.0                desc_1.3.0              Brobdingnag_1.2-6      
#>  [85] rgeos_0.5-5             Matrix_1.3-2            classInt_0.4-3         
#>  [88] vctrs_0.3.8             pillar_1.6.4            lifecycle_1.0.1        
#>  [91] furrr_0.2.2             jquerylib_0.1.3         data.table_1.13.6      
#>  [94] raster_3.5-9            R6_2.5.1                bookdown_0.24          
#>  [97] KernSmooth_2.23-18      gridExtra_2.3           parallelly_1.23.0      
#> [100] codetools_0.2-18        MASS_7.3-53.1           assertthat_0.2.1       
#> [103] rprojroot_2.0.2         withr_2.4.3             mgcv_1.8-34            
#> [106] parallel_4.0.5          hms_1.1.1               terra_1.4-22           
#> [109] grid_4.0.5              class_7.3-18            snakecase_0.11.0       
#> [112] git2r_0.29.0            base64enc_0.1-3